package cn.xzqwjw.taskmanager.controller.api;

import cn.xzqwjw.taskmanager.domain.pojo.Article;
import cn.xzqwjw.taskmanager.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @author wjw
 */
@Validated
@RestController
@RequestMapping("/api/article")
public class ArticleController {

  private final ArticleService articleService;

  @Autowired
  public ArticleController(ArticleService articleService) {
    this.articleService = articleService;
  }

  /**
   * 处理 "/api/article" 的 POST 请求，添加文章
   */
  @PostMapping("")
  public String add(
      @NotNull(message = "参数不能为空")
      @Valid @RequestBody Article article) {
    return articleService.add(article);
  }

  /**
   * 处理 "/api/article/{id:[0-9]+}" 的 DELETE 请求，逻辑删除文章
   */
  @DeleteMapping("/{idArticle:[0-9]+}")
  public String delById(
      @NotNull(message = "id不能为空")
      @Min(value = 1, message = "id必须大于0")
      @PathVariable Integer idArticle) {
    return articleService.delById(idArticle);
  }

  /**
   * 处理 "/api/article/editor/recycle/{id:[0-9]+}" 的 DELETE 请求，真实删除单篇文章
   */
  @DeleteMapping("/editor/recycle/{idArticle:[0-9]+}")
  public String delByIdReal(
      @NotNull(message = "文章id不能为空")
      @Min(value = 1, message = "文章id必须大于0")
      @PathVariable Integer idArticle) {
    return articleService.delByIdReal(idArticle);
  }

  /**
   * 处理 "/api/article/batch" 的 DELETE 请求，批量逻辑删除文章
   */
  @DeleteMapping("/batch")
  public String batchDel(
      @NotNull(message = "id不能为空")
      @RequestParam(value = "ArticleIds") String articleIds) {
    return articleService.batchDel(articleIds);
  }

  /**
   * 处理 "/api/article/editor/recycle/batch" 的 DELETE 请求，真实批量删除文章
   */
  @DeleteMapping("/editor/recycle/batch")
  public String batchDelReal(
      @NotNull(message = "id不能为空")
      @RequestParam(value = "ArticleIds") String articleIds) {
    return articleService.batchDelReal(articleIds);
  }

  /**
   * 处理 "/api/article" 的 PUT 请求，修改文章信息
   */
  @PutMapping("")
  public String modifyAll(
      @NotNull(message = "参数不能为空")
      @Valid @RequestBody Article article) {
    return articleService.modifyAll(article);
  }

  /**
   * 处理 "/api/article/49" 的 PATCH 请求，修改id为49的文章状态
   * 设置文章状态（流程2：草稿 <-> 签发）
   * action = "draft"，不需 idAdmin
   * 撤销提交审核 / 从回收站恢复文章
   * action = "uncheck"，不需 idAdmin
   * 草稿提交审核 / 已签发文章设为待审
   * action = "signed"，idAdmin 是签发管理员的id
   * 签发编辑将待审文章直接设为签发
   */
  @PatchMapping("/{idArticle:[0-9]+}")
  public String patchStatus(
      @NotNull(message = "文章id不能为空")
      @Min(value = 1, message = "文章id必须大于0")
      @PathVariable Integer idArticle,
      @NotNull(message = "管理员id不能为空")
      @RequestParam(value = "AdminId") Integer idAdmin,
      @NotNull(message = "文章状态值不能为空")
      @RequestParam(value = "Action") String action) {
    return articleService.patchStatus(idAdmin, idArticle, action);
  }

  /**
   * 单条恢复（文章移动到草稿箱）
   */
  @PatchMapping("/editor/recycle/{idArticle:[0-9]+}")
  public String unDel(
      @NotNull(message = "文章id不能为空")
      @Min(value = 1, message = "文章id必须大于0")
      @PathVariable Integer idArticle) {
    return articleService.unDel(idArticle);
  }

  /**
   * 处理 "/api/article/batch" 的 PATCH 请求，批量设置文章状态
   * 设置文章状态（流程2：草稿 <-> 签发）
   * action = "draft"，不需 idAdmin
   * 批量撤销提交审核
   * action = "uncheck"，不需 idAdmin
   * 批量将草稿提交审核 / 批量将已签发文章设为待审
   * action = "signed"，idAdmin 是签发管理员的id
   * 签发编辑批量将待审文章直接设为签发
   */
  @PatchMapping("/batch")
  public String batchPatchStatus(
      @NotNull(message = "管理员id不能为空")
      @RequestParam(value = "AdminId") Integer idAdmin,
      @NotNull(message = "文章id列表不能为空")
      @RequestParam(value = "ArticleIds") String articleIds,
      @NotNull(message = "状态不能为空")
      @RequestParam(value = "Action") String action) {
    return articleService.batchPatchStatus(idAdmin, articleIds, action);
  }

  /**
   * 处理 "/api/article/move" 的 PATCH 请求，批量移动文章
   */
  @PatchMapping("/move")
  public String batchMove(
      @NotNull(message = "栏目id不能为空")
      @Min(value = 1, message = "栏目id必须大于0")
      @RequestParam(value = "CategoryId") Integer idCategory,
      @NotNull(message = "文章id列表不能为空")
      @RequestParam(value = "ArticleIds") String articleIds) {
    return articleService.batchMove(idCategory, articleIds);
  }

  // ==================== 查询 ====================

  /**
   * 根据文章id查询得到某个文章的详情
   */
  @GetMapping("/{id:[0-9]+}")
  public String queryById(
      @NotNull(message = "id不能为空")
      @Min(value = 1, message = "id必须大于0")
      @PathVariable Integer id) {
    return articleService.queryById(id);
  }

  // ========== 普通编辑 ==========

  /**
   * 某普通编辑分页获取所有栏目或指定栏目下所有文章
   * url类似：/editor/list-page?PageCurrent=2&PageSize=3&EditorId=4&CategoryId=4
   */
  @GetMapping("/editor/list-page")
  public String listPage4Editor(
      HttpServletRequest request,
      @RequestParam(value = "PageCurrent", defaultValue = "1") Integer pageCurrent,
      @RequestParam(value = "PageSize", defaultValue = "20") Integer pageSize,
      @RequestParam(value = "Status") String status,
      @RequestParam(value = "CategoryId", defaultValue = "0") Integer idCategory,
      @NotNull(message = "编辑ID不能为空")
      @Min(value = 1, message = "编辑ID必须大于0")
      @RequestParam(value = "EditorId") Integer idEditor) {
    articleService.checkAdmin(request, idEditor);
    return articleService.listPage4Admin(pageCurrent, pageSize, 0, status, idCategory, idEditor);
  }

  /**
   * 某普通编辑分页获取所有栏目或指定栏目下所有回收站文章
   * url类似：/editor/recycle?PageCurrent=2&PageSize=3&EditorId=4&CategoryId=4
   */
  @GetMapping("/editor/recycle")
  public String listPageRecycle4Editor(
      HttpServletRequest request,
      @RequestParam(value = "PageCurrent", defaultValue = "1") Integer pageCurrent,
      @RequestParam(value = "PageSize", defaultValue = "20") Integer pageSize,
      @RequestParam(value = "CategoryId", defaultValue = "0") Integer idCategory,
      @NotNull(message = "编辑ID不能为空")
      @Min(value = 1, message = "编辑ID必须大于0")
      @RequestParam(value = "EditorId") Integer idEditor) {
    articleService.checkAdmin(request, idEditor);
    return articleService.listPage4Admin(pageCurrent, pageSize, 1, "", idCategory, idEditor);
  }

  /**
   * 某普通编辑根据标题关键字词搜索文章，返回分页数据
   * url类似：/editor/search?PageCurrent=2&PageSize=3&Keyword=三国演义&EditorId=9
   */
  @GetMapping("/editor/search")
  public String search4Editor(
      HttpServletRequest request,
      @RequestParam(value = "PageCurrent", defaultValue = "1") Integer pageCurrent,
      @RequestParam(value = "PageSize", defaultValue = "20") Integer pageSize,
      @NotBlank(message = "搜索关键字词不能为空")
      @RequestParam(value = "Keyword") String keyword,
      @NotNull(message = "编辑ID不能为空")
      @Min(value = 1, message = "编辑ID必须大于0")
      @RequestParam(value = "EditorId") Integer idEditor) {
    articleService.checkAdmin(request, idEditor);
    return articleService.search4Admin(pageCurrent, pageSize, keyword, idEditor);
  }

  // ========== 审核、签发编辑、超管调用 ==========

  /**
   * 审核、签发编辑、超管分页获取所有栏目或指定栏目下所有已审未审文章
   * url类似：/checker/list-page?PageCurrent=2&PageSize=3&CategoryId=4
   */
  @GetMapping("/checker/list-page")
  public String listPage4Checker(
      @RequestParam(value = "PageCurrent", defaultValue = "1") Integer pageCurrent,
      @RequestParam(value = "PageSize", defaultValue = "20") Integer pageSize,
      @RequestParam(value = "Status") String status,
      @RequestParam(value = "CategoryId", defaultValue = "0") Integer idCategory) {
    return articleService.listPage4Admin(pageCurrent, pageSize, 0, status, idCategory, 0);
  }

  /**
   * 审核、签发编辑、超管根据标题关键字词搜索文章，返回分页数据
   * url类似：/checker/search?PageCurrent=2&PageSize=3&Keyword=三体
   */
  @GetMapping("/checker/search")
  public String search4Checker(
      @RequestParam(value = "PageCurrent", defaultValue = "1") Integer pageCurrent,
      @RequestParam(value = "PageSize", defaultValue = "20") Integer pageSize,
      @NotBlank(message = "搜索关键字词不能为空")
      @RequestParam(value = "Keyword") String keyword) {
    return articleService.search4Admin(pageCurrent, pageSize, keyword, 0);
  }

}
